package cn.iocoder.yudao.module.aiposter.utils;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class MinioFileUploader {
    @Test
    public void testUpload() throws IOException, NoSuchAlgorithmException, InvalidKeyException {

        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://10.128.172.93:52710")
                            .credentials("1y71OwYhuY3zqGA4eLhh", "MDO30v8GEOVS4XskDaQ43cAUl5sdlm7GjH0wCjx8")
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("cw-poster").build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("cw-poster").build());
            } else {
                System.out.println("Bucket 'cw-poster' already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("cw-poster")
                            .object("clean_new.sql")
                            .filename("/Users/rain/work_space/java_space/ruoyi-vue-pro/cw-module-poster/cw-module-poster-biz/src/test/resources/sql/clean.sql")
                            .build());
            System.out.println(
                    "'clean.sql' is successfully uploaded as "
                            + "object 'clean_new.sql' to bucket 'cw-poster'.");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        }
    }
}
